﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    public sealed class ReferenceDict
    {
       // private static WeakDictionary<int, SpriteReference> spriteRefWeak = new WeakDictionary<int, SpriteReference>();
        private static WeakDictionary<int, PrefabReference> prefabRefWeak = new WeakDictionary<int, PrefabReference>();
        private static List<GameObject> waitClearPrefabList = new List<GameObject>();
        //private static List<Sprite> spriteList = new List<Sprite>();
        //private static List<Texture> textureList = new List<Texture>();
        private static float curUpdateTime = 0f;
        private static float defaultUpdateTime = 5f;

        //public static SpriteReference AddSpriteRefrenceWeak(Image image, Sprite sprite)
        //{
        //    int _id = image.GetInstanceID();

        //    if (!spriteRefWeak.TryGetValue(_id, out SpriteReference sr))
        //    {
        //        sr = new SpriteReference();
        //        spriteRefWeak[_id] = sr;
        //    }

        //    sr.SetSprite(sprite);
        //    return sr;
        //}

        /// <summary>
        /// 添加弱引用记录，以便弱引用也引用一份prefab
        /// </summary>
        /// <param name="id"></param>
        /// <param name="prefab"></param>
        /// <returns></returns>
        //public static PrefabReference AddPrefabReferenceWeak(int id, GameObject prefab)
        //{
        //    if (!prefabRefWeak.TryGetValue(id, out PrefabReference sr))
        //    {
        //        sr = new PrefabReference();
        //        prefabRefWeak[id] = sr;
        //    }

        //    sr.SetPrefab(prefab);
        //    return sr;
        //}

        //public static void AddNeedClearSprite(Sprite sprite)
        //{
        //    lock (spriteList)
        //    {
        //        spriteList.Add(sprite);
        //    }
        //}
        //public static void AddNeedClearTexture(Texture texture)
        //{
        //    lock (textureList)
        //    {
        //        textureList.Add(texture);
        //    }
        //}
        
        /// <summary>
        /// 添加需要清除引用计数的Prefab
        /// </summary>
        /// <param name="prefab"></param>
        public static void AddNeedClearPrefab(GameObject prefab)
        {
            lock (waitClearPrefabList)
            {
                waitClearPrefabList.Add(prefab);
            }
        }

        public static void Update()
        {
            //lock (spriteList)
            //{
            //    if (spriteList.Count > 0)
            //    {
            //        for (int i = spriteList.Count - 1; i >= 0; i--)
            //        {
            //            SpritePool.Instance.Free(spriteList[i], false);
            //        }
            //        spriteList.Clear();
            //    }

            //}
            //lock (textureList)
            //{
            //    if (textureList.Count > 0)
            //    {
            //        for (int i = textureList.Count - 1; i >= 0; i--)
            //        {
            //            TexturePool.Instance.Free(textureList[i], false);
            //        }
            //        textureList.Clear();
            //    }

            //}
            

            //检测是否有要清除引用计数的prefab
            lock (waitClearPrefabList)
            {
                if(waitClearPrefabList.Count > 0)
                {
                    for (int i = waitClearPrefabList.Count - 1; i >= 0; i--)
                    {
                        PrefabPool.Instance.Free(waitClearPrefabList[i], false);
                    }
                    waitClearPrefabList.Clear();
                }
            }

            if (Time.time - curUpdateTime >= defaultUpdateTime)
            {
                curUpdateTime = Time.time;
               // spriteRefWeak.GC();
                prefabRefWeak.GC();
            }

        }










    }
}

